package cz.proks.easyhelpdesk.gwt.activity;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.PlaceHistoryMapper;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

import cz.proks.easyhelpdesk.gwt.ClientFactory;
import cz.proks.easyhelpdesk.gwt.Globals;
import cz.proks.easyhelpdesk.gwt.activity.base.RestrictedActivity;
import cz.proks.easyhelpdesk.gwt.place.UserPlace;
import cz.proks.easyhelpdesk.gwt.ui.UserView;
import cz.proks.easyhelpdesk.gwt.ui.base.RestrictedView;

public class UserActivity extends RestrictedActivity<UserPlace> implements UserView.Presenter{
    private final UserView view;

    public UserActivity(UserPlace place, ClientFactory clientFactory, PlaceHistoryMapper placeMapper) {
        super(place, clientFactory, placeMapper);
        this.view = clientFactory.getUserView();
    }

    @Override
    protected RestrictedView getView() {
        return view;
    }
    
    @Override
    protected boolean hasAccess() {     
        return super.hasAccess() && Globals.getCurrentUser().getId().equals(getPlace().getUserId());
    }

    @Override
    protected void onPermission(AcceptsOneWidget panel, EventBus eventBus) {
        view.setPresenter(this);
        view.setUser(Globals.getCurrentUser());
        
        panel.setWidget(view);
    }
    
    @Override
    public void back() {
        History.back();        
    }
}
